﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;
using System.Data;
using Dapper;

namespace Zor_DAL
{
  public  class Zor_Summary_DAL<T> where T : class
    {
        /// <summary>
        /// 汇总
        /// </summary>
        /// <param name="accpid">受理单ｉｄ</param>      
        /// <param name="Sm_Id">汇总主键ｉｄ</param>
        /// <param name="actualtax">汇总金额</param>
        /// <param name="actualtax">楼宇id</param>
        /// 受理状态(0保存,1提交2被退回、3已评议4已评定5、已审核、6已缴税、7、已开票8、已消号、9已汇总)
        /// <returns></returns>
      public static BaseAnswer UpdateSummary(string Sm_Id, string accpid, int Grsds, int Fcs, int actualtax, string Bdid)
      {
          var sql = "select Dt_AmdCode from [dbo].[Sys_Department] where [Dt_Id]=@Dt_Id";
          Sys_Department Department = DapperQuery<Sys_Department>.DoQueryT(sql, new { Dt_Id = RequestSession.GetSessionUser().DT_Id });
          //生产汇总单号
          var Sm_Number = "C" + Department.Dt_AmdCode.ToString().Substring(0, 3) + DateTime.Now.Year.ToString().Substring(DateTime.Now.Year.ToString().Length - 2) + DateTime.Now.ToString("mmddhhmmss");
          //新增消号管理表数据
          var sqlinsert = "insert into Zor_Disannul ([Fcs],[Grsds],[Dn_Number] ,[Dn_ActtualTax] ,[Dn_State] ,[Dn_Date] ,[Dn_UserId],Acceptance_Id,Bd_Id) output  inserted.Dn_Id  VALUES(" + Fcs + "," + Grsds + ",'" + Sm_Number + "'," + actualtax + ",1," + TimeHelper.ToTimeStamp(DateTime.Now) + "," + RequestSession.GetSessionUser().User_Id + ",'" + accpid + "','" + Bdid + "')";
          var str = DapperQuery<dynamic>.DoQueryString(sqlinsert);//获取新增的自动标识
          List<string> lstsql = new List<string>();
          //更新受理状态操作时间，操作人
          lstsql.Add(string.Format(" update [Zor_Summary] set  Dn_Id=" + str + " ,Sm_State=1,Sm_Date=" + TimeHelper.ToTimeStamp(DateTime.Now) + ",User_Id=" + RequestSession.GetSessionUser().User_Id + " where Sm_Id in (" + Sm_Id + ")"));
          //更新受理单状态
          lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State=9 where Acceptance_Id in (" + accpid + ")"));
          //修改缴税管理-受理状态
          lstsql.Add(string.Format("update [Zor_PayTaxes] set Pt_State=2 where Examine_Id in ((select Examine_Id from dbo.Zor_Examine where Evaluate_Id in (select Evaluate_Id from dbo.Zor_Evaluate where Review_Id in (select Review_Id from Zor_Review where Acceptance_Id in (" + accpid + ") ))))"));
          //修改发票号管理-受理状态
          lstsql.Add(string.Format("update [Zor_Invoice] set In_State=2 where Pt_Id in (select Pt_id from Zor_PayTaxes where  Examine_Id in ((select Examine_Id from dbo.Zor_Examine where Evaluate_Id in (select Evaluate_Id from dbo.Zor_Evaluate where Review_Id in (select Review_Id from Zor_Review where Acceptance_Id in (" + accpid + ") )))))"));
          BaseAnswer bar = DapperTransaction.DoTransaction(lstsql);
          return bar;
      }
        /// <summary>
        /// 取消汇总
        /// </summary>
        /// <param name="accpid">受理单ｉｄ</param>      
        /// <param name="Sm_Id">汇总主键ｉｄ 汇总状态（0保存（取消汇总），1待消号（汇总），2已消号,）</param>
        /// 受理状态(0保存,1提交2被退回、3已评议4已评定5、已审核、6已缴税、7、已开票8、已消号、9已汇总)
        /// <returns></returns>
      public static BaseAnswer CancelSummary(int Dn_Id, string accpid)
      {
          List<string> lstsql = new List<string>();
          //修改汇总表状态和消号表id 
          lstsql.Add(string.Format(" update [Zor_Summary] set  Sm_State=0,Dn_Id=0  where Dn_Id = " + Dn_Id + ""));
          //删除消号表信息
          lstsql.Add(string.Format(" delete from  Zor_Disannul  where Dn_Id = " + Dn_Id + ""));
          //更新受理单状态
          lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State=7 where Acceptance_Id in (" + accpid + ")"));
          BaseAnswer bar = DapperTransaction.DoTransaction(lstsql);
          return bar;
      }

      ///// <summary>
      ///// 获取汇总操作列表
      ///// </summary>
      ///// <returns></returns>
      //public static List<T> LoadSummaryOperation()
      //{
      //    var sql = "select * from  V_Zor_Summary order by In_Date desc ";
      //    List<T> listSummaryOperation = DapperQuery<T>.DoQueryList(sql, null);
      //    return listSummaryOperation;
      //}

      /// <summary>
      /// 打印列表
      /// </summary>
      /// <returns></returns>
      public static List<T> Print_Summary(int Dn_Id)
      {
          var sql = "select *,row_number() over (order by  Acceptance_TicketNumber) as rowId from [V_Print_Summary] where Dn_Id = "+Dn_Id+" ";
          List<T> listDtName = DapperQuery<T>.DoQueryList(sql, null);
          return listDtName;
      }
      /// <summary>
      ///  打印份数
      /// </summary>
      /// <returns></returns>
      public static List<T> GetCopies(int Id)
      {
          var sql = "select count(0) as Copies from [V_Print_Summary] where Dn_Id = " + Id + "";
          List<T> list = DapperQuery<T>.DoQueryList(sql, null);
          return list;
      }

      /// <summary>
      ///  打印总金额
      /// </summary>
      /// <returns></returns>
      public static List<T> GetSumMoney(int Id)
      {
          var sql = "select sum(ETotalTax) as SumMoney from [V_Print_Summary] where Dn_Id = " + Id + "";
           List<T> list = DapperQuery<T>.DoQueryList(sql, null);
           return list;
      }
      /// <summary>
      ///  汇总时间
      /// </summary>
      /// <returns></returns>
      public static List<T> GetSm_Date(int Id)
      {
          var sql = "select max(Sm_Date) as Sm_Date from [V_Print_Summary] where Dn_Id = " + Id + "";
          List<T> list = DapperQuery<T>.DoQueryList(sql, null);
          return list;
      }

    }
}